//
//  PopularViewController.m
//  NewMyMusicAPP
//
//  Created by 11_wyq on 15/6/19.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PopularViewController.h"

@interface PopularViewController ()
@property (retain ,nonatomic) MBProgressHUD *MBP;
@end

@implementation PopularViewController

-(void)viewWillAppear:(BOOL)animated
{
     
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor blackColor];
    
    
    //       给导航栏上放自定义label
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,WIDTH * 100, HEIGHT * 30)]autorelease];
    [view setBackgroundColor:[UIColor clearColor]];
    self.navigationItem.titleView = view;
    UILabel *title = [[[UILabel alloc]initWithFrame:CGRectMake(WIDTH * 30, 0, WIDTH * 50,HEIGHT * 30)]autorelease];;
    title.text = @"首页";
    title.textColor = [UIColor whiteColor];
    [title setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];//加粗字体
    [view addSubview:title];
    
    
    
    
    _headerPage = 1;
    _pageNumber = 0;
    self.array=[NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    UIImage *image2 = [UIImage imageNamed:@"menu.png"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(locker)];
    
    //搜索
    UIImage *image3 = [UIImage imageNamed:@"Search@3x.png"];
    image3 = [image3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:image3 style:UIBarButtonItemStylePlain target:self action:@selector(searchBar)]autorelease] ;
    
    self.myTableView=[[[UITableView alloc]initWithFrame:CGRectMake(0,WIDTH * 44, MYWIDTH,HEIGHT * 559) style:UITableViewStylePlain]autorelease];
    self.myTableView.backgroundColor=[UIColor blackColor];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.delegate=self;
    self.myTableView.dataSource=self;
    
    self.myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;//cell隐藏线
    
    self.myView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, MYWIDTH,HEIGHT * 52)]autorelease];
    self.myView.backgroundColor=[UIColor blackColor];
    [self.view addSubview:self.myView];
    
    //建Button 调方法
    [self makeButton];
    //建数组
    [self dictionaryAndNumber:1];
    
    //上面的滚动视图
    
    [self requestData];
    //默认显示"全部"的内容
    
    //动态小黄人
    [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
    [GiFHUD show];
    
    self.isUpLoading = NO;
    [self addHeader:1];//下拉刷新
    [self addFooter:1 ];//上啦加载更多
    
   // NSLog(@"333333");
      [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
}


-(void)dictionaryAndNumber:(NSInteger)number
{
    
    _preDic = [NSMutableDictionary dictionary];//接口字典
    
    
    
    if (number == 1 ) {
        [_preDic setObject:@"0" forKey:@"D-A"];
        [_preDic setObject:@"POP_ALL" forKey:@"area"];
        [_preDic setObject:@"0" forKey:@"offset"];
        [_preDic setObject:@"20" forKey:@"size"];
        
    } if (number==2)
    {
        
        [_preDic setObject:@"0" forKey:@"D-A"];
        [_preDic setObject:@"POP_ML" forKey:@"area"];
        [_preDic setObject:@"0" forKey:@"offset"];
        [_preDic setObject:@"20" forKey:@"size"];
        
        
    } if (number==3)
    {
        [_preDic setObject:@"0" forKey:@"D-A"];
        [_preDic setObject:@"POP_HT" forKey:@"area"];
        [_preDic setObject:@"0" forKey:@"offset"];
        [_preDic setObject:@"20" forKey:@"size"];
        
        
    } if (number==4)
    {
        [_preDic setObject:@"0" forKey:@"D-A"];
        [_preDic setObject:@"POP_JP" forKey:@"area"];
        [_preDic setObject:@"0" forKey:@"offset"];
        [_preDic setObject:@"20" forKey:@"size"];
        
        
    } if (number==5)
    {
        [_preDic setObject:@"0" forKey:@"D-A"];
        [_preDic setObject:@"POP_KR" forKey:@"area"];
        [_preDic setObject:@"0" forKey:@"offset"];
        [_preDic setObject:@"20" forKey:@"size"];
        
        
    } if (number==6)
    {
        [_preDic setObject:@"0" forKey:@"D-A"];
        [_preDic setObject:@"POP_US" forKey:@"area"];
        [_preDic setObject:@"0" forKey:@"offset"];
        [_preDic setObject:@"20" forKey:@"size"];
        
        
    }
    
    
}



//下拉刷新
-(void)addHeader:(NSInteger)number
{
    __block PopularViewController *popilarVC=self;
    
    
    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        popilarVC.isUpLoading=NO;
        [self dictionaryAndNumber:number];//创建数组
        [popilarVC requestData];
    }];
    
    //一进程序自动刷新
    
    //[self.myTableView.header beginRefreshing];
}

//上啦刷新
-(void)addFooter:(NSInteger)number
{
    __block PopularViewController *popilarVC=self;
    self.myTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        popilarVC.isUpLoading=YES;
        
        
        _pageNumber = _pageNumber + 20;
        NSString *newPage = [NSString stringWithFormat:@"%ld",(long)_pageNumber ];
        NSLog(@"newPage = %@",newPage);
        _preDic = [NSMutableDictionary dictionary];//接口字典
        
        [self dictionaryAndNumber:number];
        [_preDic setObject:newPage forKey:@"offset"];
        
        popilarVC.nextPage ++;
        [popilarVC requestData];
        
        
    }];
    
}



//建Button方法
-(void)makeButton
{
    
    
    
    _nameArray  = [NSMutableArray arrayWithObjects:@"全部",@"大陆",@"港台",@"日本",@"韩国",@"欧美", nil];
    
    for (int i = 0; i < 6; i++) {
        NSString *string = [_nameArray objectAtIndex:i];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake((10 + (i * 42 ) + (i * 20)) * WIDTH, 0, 50 *WIDTH, 40 * HEIGHT);
        [self.myView addSubview:button];
        button.tag = 1000 + i;
        [button setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];//加粗字体
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitle:string forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor colorWithHexString:@"FF9966"] forState:UIControlStateNormal];
    
    
}
-(void)buttonAction:(UIButton *)button
{
    
    _pageNumber = 0;//下拉加载
    
    
    if (button == (UIButton *)[self.view viewWithTag:1000]) {
        
        self.array = [NSMutableArray array];
        [self dictionaryAndNumber:1];
        [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1005] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addHeader:1];//下拉刷新
        [self addFooter:1];//上啦加载更多
        [self.myTableView reloadData];
        
    }
    if (button == (UIButton *)[self.view viewWithTag:1001]) {
        
        
        self.array = [NSMutableArray array];
        [self dictionaryAndNumber:2];
        
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1005] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addHeader:2];//下拉刷新
        [self addFooter:2];//上啦加载更多
         [self.myTableView reloadData];
    }
    if (button == (UIButton *)[self.view viewWithTag:1002]) {
        
        
        self.array = [NSMutableArray array];
        [self dictionaryAndNumber:3];
        
        [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1005] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addHeader:3];//下拉刷新
        [self addFooter:3];//上啦加载更多
         [self.myTableView reloadData];
    }
    if (button == (UIButton *)[self.view viewWithTag:1003]) {
        
        self.array = [NSMutableArray array];
        [self dictionaryAndNumber:4];
        
        [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1005] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addHeader:4];//下拉刷新
        [self addFooter:4];//上啦加载更多
         [self.myTableView reloadData];
        
    }
    if (button == (UIButton *)[self.view viewWithTag:1004]) {
        
        self.array = [NSMutableArray array];
        [self dictionaryAndNumber:5];
        
        [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1005] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addHeader:5];//下拉刷新
        [self addFooter:5];//上啦加载更多
         [self.myTableView reloadData];
    }
    if (button == (UIButton *)[self.view viewWithTag:1005]) {
        
        self.array = [NSMutableArray array];
        [self dictionaryAndNumber:6];
        
        [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addHeader:6];//下拉刷新
        [self addFooter:6];//上啦加载更多
         [self.myTableView reloadData];
    }
    [button setTitleColor:[UIColor colorWithHexString:@"FF9966"] forState:UIControlStateNormal];
    //菊花
    [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
    [GiFHUD show];
    [self requestData];
    
    
    
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.array.count != 0) {
        return self.array.count;
    }else
    {
        return 0;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *myCell=@"cell";
    PopularViewCell *cell=[tableView dequeueReusableCellWithIdentifier:myCell];
    if (cell == nil) {
        cell = [[[PopularViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCell]autorelease];
        
        
    }
    
    
//    cell.popularModel = [self.array objectAtIndex:indexPath.row];
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    //渐变效果
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.alpha = 0;
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:1];
    cell.alpha = 1;
    [UIView commitAnimations];
    
    if (self.array.count !=0) {
        PopularModel *popuModel=[self.array objectAtIndex:indexPath.row];
        
        cell.popularModel = [self.array objectAtIndex:indexPath.row];
        [[DataHandle shareHandleController]openDB];
        [[DataHandle shareHandleController]createCollectTable];
        NSMutableArray *array = [[DataHandle shareHandleController]selectCollect];
        if (array.count == 0) {
            cell.buer=NO;
        }else
        {
            for (CollectModel *collect  in array) {
                NSString *modeID = [NSString stringWithFormat:@"%@",popuModel.idd];
                if ([collect.modelId isEqualToString:modeID]) {
                    cell.buer= YES;
                    cell.popularModel =[self.array objectAtIndex:indexPath.row];
                    return cell;
                }
            }
            cell.buer=NO;
            cell.popularModel=[self.array objectAtIndex:indexPath.row];
            
        }
   
    }
    

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return HEIGHT * 200;
    
}
//点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([DataHandle shareHandleController].monitorNetwork ==NO) {
        UIAlertView *alertView =[ [UIAlertView alloc]initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];

        
    }else
    {
        VideoPlayerViewController *videoPlayer = [[[VideoPlayerViewController alloc]init]autorelease];
        PopularModel *premereModel=[self.array objectAtIndex:indexPath.row];
        videoPlayer.Id = premereModel.idd;
        videoPlayer.type = @"VIDEO";
        videoPlayer.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:videoPlayer animated:YES];
        

    }
  
}
//请求数据
-(void)requestData
{
    
    
    NSMutableDictionary *myDic = [NSMutableDictionary dictionaryWithDictionary:_preDic];

    
    
   
    
    [Connect ConnectRequestAFWithURL:Popular params:_preDic requestHeader:RequestHeader httpMethod:@"GET" block:^(NSObject *result) {
        //  NSLog(@"result = %@",result);
        
        NSMutableArray *resArray = [(NSMutableDictionary *)result objectForKey:@"videos"];
        //NSLog(@"array = %@",resArray);
        //需要判断请没请求下来数据
        if (result == nil) {

           // [GiFHUD dismiss];//动态图隐藏
          //  [self.MBP hide:YES];//菊花隐藏


            [self.myTableView.header endRefreshing];//结束下拉刷新
            [self.myTableView.footer endRefreshing];//结束上拉加载
            self.array=[[DataHandle shareHandleController] selectPopular];
            
            [self.myTableView reloadData];
            UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            [aler show];
            [GiFHUD dismiss];//隐藏小黄人动态图
            [DataHandle shareHandleController].monitorNetwork=NO;
        }else{
            
            
            [[DataHandle shareHandleController]openDB];
            [[DataHandle shareHandleController] dropPopularTable];//删除表
            [[DataHandle shareHandleController] createPopularTable];//创建表
        
        for (NSMutableDictionary *dic in resArray) {
            
            PopularModel *popularModel = [[[PopularModel alloc]init]autorelease];
            popularModel.idd= [dic objectForKey:@"id"];
            popularModel.title=[dic objectForKey:@"title"];
            popularModel.artistName = [dic objectForKey:@"artistName"];
            popularModel.albumImg = [dic objectForKey:@"albumImg"];
            popularModel.posterPic = [dic objectForKey:@"posterPic"];
            popularModel.type=@"VIDEO";
            
        
            
            if ([[myDic objectForKey:@"area"] isEqualToString:@"POP_ALL"]) {
                
                [[DataHandle shareHandleController] insertPopular:popularModel];//插入数据
            }

            
            [self.array addObject:popularModel];
            
            [DataHandle shareHandleController].monitorNetwork=YES;
            [PopularModel release];
            
        }
        //NSLog(@"array = %@",self.array);
        
         [GiFHUD dismiss];//动态图隐藏
            
        [self.myTableView.header endRefreshing];//结束下拉
        
        [self.myTableView.footer endRefreshing];//结束上啦
        
        [self.myTableView reloadData];
        }
    }];
    
    
    
    
}

//搜索
-(void)searchBar
{
    [GiFHUD dismiss];//隐藏小黄人动态图
    SearchViewController *search = [[[SearchViewController alloc]init]autorelease];
    search.hidesBottomBarWhenPushed = YES;//隐藏Bar
    
    [self.navigationController pushViewController:search animated:YES];
    
}


-(void)locker
{
    [GiFHUD dismiss];//动态图隐藏 
    [self.sideMenuViewController presentLeftMenuViewController];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
